package cn.falcon.fmobi.console.controller;

import cn.falcon.fmobi.biz.service.AudienceLikeService;
import cn.falcon.fmobi.biz.service.AudienceService;
import cn.falcon.fmobi.console.common.RestfulJsonModelAndView;
import cn.falcon.fmobi.console.common.Result;
import cn.falcon.fmobi.console.constant.ParamConstant;
import cn.falcon.fmobi.console.constant.UrlConstant;
import cn.falcon.fmobi.console.exception.ParamValidateException;
import cn.falcon.fmobi.dao.model.AddAudienceLikeVo;
import cn.falcon.fmobi.dao.po.Audience;
import cn.falcon.marketing.api.util.ValidateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;

/**
 * Created by haojie.zhang
 * Time 2017/9/21.
 */

@Controller
@RequestMapping(UrlConstant.AUDIENCE_LIKE_URL)
public class AudienceLikeCtrl extends BaseCtrl{

    @Autowired
    private AudienceLikeService audienceLikeService;

    @Autowired
    private AudienceService audienceService;

    @Value("${accountId}")
    private String accountId;
    /**
     * 创建相似人群
     * @param addAudienceLikeVo
     * @return
     */
    @RequestMapping(value = "/addAudienceLike",method = {RequestMethod.POST})
    public ModelAndView addAudience(@RequestBody AddAudienceLikeVo addAudienceLikeVo)throws UnsupportedEncodingException, ParamValidateException{

        addAudienceLikeVo.setAccountId(Integer.parseInt(accountId));
        // 查询代理商id和广点通人群id
        Audience audience = audienceService.getAudience(addAudienceLikeVo.getSeedAudienceId());
        addAudienceLikeVo.setMarketingAudienceId(audience.getMarketingAudienceId());
        addAudienceLikeVo.setAdvertiserId(audience.getAdvertiserId());

        this.validateAdvertiserParam(addAudienceLikeVo);

        audienceLikeService.addAudienceLike(addAudienceLikeVo);
        return RestfulJsonModelAndView.buildJsonModelAndView();
    }

    /**
     * 人群参数验证
     * @param addAudienceLikeVo
     */
    public  void validateAdvertiserParam(AddAudienceLikeVo addAudienceLikeVo)throws ParamValidateException, UnsupportedEncodingException {
        //名称是否唯一
        int count = audienceService.countName(addAudienceLikeVo.getName(),addAudienceLikeVo.getAdvertiserId(),null);
        if (count != 0) {
            throw new ParamValidateException(Result.ERR_INVALID_PARAMETER, "crowdName", "Result.err.repeat.parameter");
        }
        //目标人数
        if (addAudienceLikeVo.getExpandUserCount() < ParamConstant.AUDIENCE_EXPAND_USER_COUNT_MIX
                || addAudienceLikeVo.getExpandUserCount() > ParamConstant.AUDIENCE_EXPAND_USER_COUNT_MAX) {
            throw new ParamValidateException(Result.ERR_INVALID_LENGTH, "ExpandUserCount", "Result.err.invalid.length");
        }
        //人群名称必填
        if(StringUtils.isEmpty(addAudienceLikeVo.getName())){
            throw new ParamValidateException(Result.ERR_MISS_PARAMETER, "crowdName", "Result.err.miss.parameter");
        }
        //名称长度限制
        if (ValidateUtil.checkStrByteLengthLessThan(addAudienceLikeVo.getName(), ParamConstant.CROWD_NAME_LENGTH_MIN)
                || ValidateUtil.checkStrByteLengthMoreThan(addAudienceLikeVo.getName(), ParamConstant.CROWD_NAME_LENGTH_MAX)) {
            throw new ParamValidateException(Result.ERR_INVALID_LENGTH, "crowdName", "Result.err.invalid.length");
        }
        //描述最大长度
        if (ValidateUtil.checkStrByteLengthMoreThan(addAudienceLikeVo.getDescription(),ParamConstant.CROWD_DESCRIPTION_LENGTH_MAX)){
            throw new ParamValidateException(Result.ERR_INVALID_LENGTH, "crowdDescription", "Result.err.invalid.length");
        }
        if (0 == (addAudienceLikeVo.getSeedAudienceId())) {
            throw new ParamValidateException(Result.ERR_MISS_PARAMETER, "seedAudienceId", "Result.err.miss.parameter");
        }
    }
}
